package LeetCode.month04.day104;

/**
 * @author 16778
 * @date 2025/11/14 8:25
 * @description
 */
public class No2536 {
    public int[][] rangeAddQueries(int n, int[][] queries) {
        // 二维差分
        int[][] diff = new int[n + 2][n + 2];
        for (int[] q : queries) {
            int r1 = q[0], c1 = q[1], r2 = q[2], c2 = q[3];
            diff[r1 + 1][c1 + 1]++;
            diff[r1 + 1][c2 + 2]--;
            diff[r2 + 2][c1 + 1]--;
            diff[r2 + 2][c2 + 2]++;
        }

        // 原地计算 diff 的二维前缀和，然后填入答案
        int[][] ans = new int[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                diff[i + 1][j + 1] += diff[i + 1][j] + diff[i][j + 1] - diff[i][j];
                ans[i][j] = diff[i + 1][j + 1];
            }
        }
        return ans;
    }

    public int[][] rangeAddQueries1(int n, int[][] queries) {
        int[][] res = new int[n][n];
        int len = queries.length;
        for (int i = 0; i < len; i++) {
            int x1 = queries[i][0];
            int y1 = queries[i][1];
            int x2 = queries[i][2];
            int y2 = queries[i][3];
            for (int j = x1; j <= x2; j++) {
                res[j][y1]++;
                if(y2+1<n){
                    res[j][y2+1]--;
                }
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 1; j < n; j++) {
                res[i][j]+=res[i][j-1];
            }
        }
        return res;
    }
    public int[][] rangeAddQueries2(int n, int[][] queries) {
        int[][] res = new int[n][n];
        int len = queries.length;
        for (int i = 0; i < len; i++) {
            int x1 = queries[i][0];
            int y1 = queries[i][1];
            int x2 = queries[i][2];
            int y2 = queries[i][3];
            for (int j = x1; j <= x2; j++) {
                for (int k = y1; k <= y2; k++) {
                    res[j][k]++;
                }
            }
        }
        return res;
    }
}
